<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>售货订单</title>
    <link rel="stylesheet" href="${ctx}/resource/layui/css/layui.css">
</head>
<body>
<form class="layui-form " onsubmit="return false">
    <div class="layui-form-item">
        <label class="layui-form-label">商品名</label>
        <div class="layui-input-inline">
            <select lay-verify="required" lay-filter="goodName" name="goodID" lay-search id="goodName">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数量</label>
        <div class="layui-input-inline">
            <input type="text" id="number" name="number" required  lay-verify="required" placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">平均进价</label>
        <div id="averagePurchasePrice"> </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-inline">
            <input type="text" name="unitPrice" required  lay-verify="required" placeholder="请输入输入框内容" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="sbBtn">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="${ctx}/resource/layui/layui.js"></script>
<script type="text/javascript">
    //JavaScript代码区域
    layui.use(['element','form','jquery'], function(){

        //----------模块----------
        var form = layui.form,$=layui.$;

        //动态添加下拉框 同时可以设置默认值
        $.ajax({
            url:'good.do?type=getList',
            dataType:'json',
            type:'post',
            success:function(data){
                if (data.code == 200) {
                    $("#goodName").empty();
                    $("#goodName").append(new Option("请选择商品", ""));
                    $.each(data.data, function(index, item) {
                        var value = item.goodsType+'-'+item.brand+'-'+item.goodName+'('+item.providerName+')'
                        $('#goodName').append(new Option(value,item.iD));
                    });
                } else {
                    $("#goodName").append(new Option("暂无数据", ""));
                }
                form.render("select");
            }
        });


        form.on('select(goodName)', function (data) {
            var goodID = data.value;
            var param = {"goodID":goodID}
            console.log(goodID)
            $.post("good.do?type=getByID",param,function(result){
                if(result.code == 200){
                    $("#averagePurchasePrice").empty();
                    $("#averagePurchasePrice").append("<div class='layui-form-label'>" + result.data.averagePurchasePrice+"</div>");
                }else{
                    alert("Error");
                }
            });
            return false;
        });


        $.ajax({
            url:'good.do?type=getList',
            dataType:'json',
            type:'post',
            success:function(data){
                if (data.code == 200) {
                    $("#goodName").empty();
                    $("#goodName").append(new Option("请选择商品", ""));
                    $.each(data.data, function(index, item) {
                        var value = item.goodsType+'-'+item.brand+'-'+item.goodName+'('+item.providerName+')'
                        $('#goodName').append(new Option(value,item.iD));
                    });
                } else {
                    $("#goodName").append(new Option("暂无数据", ""));
                }
                form.render("select");
            }
        });

        form.on('submit(sbBtn)', function(data){
            var param = data.field;
            $.post("sealOrder.do?service=addOrder",param,function(result){
                //登录正常  进行跳转
                if(result.code == 200){
                    // layer.msg("订单添加成功");
                    alert("订单添加成功");
                }else{
                    //登录不正常  提示 错误信息
                    // layer.msg(result.msg);
                    alert("Error");
                }
            });
            return false;

            // alert(data.form.val("goodID"));
        });
    });
</script>
</body>
</html>

